package wint.lang.utils;


public class HtmlUtil {
	
	public static final String RICH_TEXT_PREFIX = "¤";
	
	public static String richtext(String input) {
		if (StringUtil.isEmpty(input)) {
			return RICH_TEXT_PREFIX;
		}
		return RICH_TEXT_PREFIX + input;
	}
	
	public static String escapeHtml(String input) {
		if (StringUtil.isEmpty(input)) {
			return input;
		}
		StringBuilder sb = new StringBuilder();
		final int len = input.length();
		for (int i = 0; i < len; ++i) {
			char c = input.charAt(i);
			switch(c) {
			case '&':
				sb.append("&amp;");
				break;
			case '<':
				sb.append("&lt;");
				break;
			case '>':
				sb.append("&gt;");
				break;
			case '\"':
				sb.append("&quot;");
				break;	
			case '\'':
				sb.append("&apos;");
				break;	
			default:
				sb.append(c);
			}
		}
		return sb.toString();
	}
	
	
	public static String unescapeHtml(String input) {
		if (StringUtil.isEmpty(input)) {
			return input;
		}
		input = input.replaceAll("&apos;", "\'");
		input = input.replaceAll("&quot;", "\"");
		input = input.replaceAll("&gt;", ">");
		input = input.replaceAll("&lt;", "<");
		input = input.replaceAll("&amp;", "&");
		return input;
	}

}
